<?php
/**
 * @property CI_Loader $load
 * @property CI_Form_validation $form_validation
 * @property CI_Input $input
 * @property CI_Email $email
 * @property CI_DB_active_record $db
 * @property Telephone
 */
class Customer extends CI_Controller {
    var $phoneno;
    function  __construct() {
        parent::__construct();
        $this->phoneno = $this->session->userdata('phoneno');
        $this->load->helper('form');
        $this->load->model('Telephone');
        $this->load->model('Employee');
        $this->load->model('Shiftconnection');
        $this->load->model('Shifthandle');
    }
     function shift()
    {
        $this->entrance();
        $data["title"]="Shift Connection";
        $data["content"]="register/shift_content";
        $data["sidebar"]="register/shift_sidebar";
        $data["js"]=array("shift_connection.js");
        $this->load->view("template",$data);
    }
    function entrance()
    {

        if($this->session->userdata('logged_in')!=null && $this->session->userdata('rank') == 'customer' )
        {
            $this->phoneno = $this->session->userdata('phoneno');
            return 1;
        }
        else
        {
            redirect('home');
        }
    }

    function submit_shift() {

        $this->entrance();
        $newaddress = $_POST['newaddress'];
        $causeshift = $this->input->post('causeshift');
        $code = $this->Telephone->get_code($this->phoneno);
        $empid = $this->Employee->get_DE($code);

        if( !$this->Shiftconnection->already_apply($this->phoneno)) {
            $this->db->trans_start();
            $appid = $this->Shiftconnection->insert($newaddress,$this->phoneno,$causeshift);
            $this->Shifthandle->insert($appid,$empid,$code);
            //TODO:check for already apply

            $this->db->trans_complete();

            $this->confirm("Your Application number is ".$appid.". Remember this and use for checking status of your Application.");
        }
        else {
            $this->confirm("You have already applied for shift connection.");
        }


    }

    function confirm($msg="Your Application no is 420") {
        $data["title"]="Confirmation";
        $data["content"]="register/confirm";
        $data["sidebar"]="admin/sidebar";
        $js[]="admin.js";
        $js[]="jquery.validate.js";
        $data["js"]=$js;
        $data["msg"]=$msg;
        $this->load->view("template",$data);
    }
     function shift_check_status()
    {
        $this->entrance();
        $data["title"]="Check Status";
        $data["content"]="register/shift_check_status";
        $data["sidebar"]="admin/sidebar";
         $js[]="shift_check_status.js";
         $data["js"]=$js;
         $this->load->view("template",$data);
    }
    function shift_fetch_ajax()
    {

      //  $data["appid"]="sds";
         $this->entrance();
        echo json_encode($this->Shiftconnection->get_status($_POST));
    }

    /**************Complain****************************/
    function complain()
    {
        $this->entrance();
        $data["title"]="Complain";
        $data["content"]="register/complain_content";
        $data["sidebar"]="register/complain_sidebar";
        $data["js"]=array("complain.js");
        $this->load->model("Complain");
        $data["category"]=$this->Complain->get_category();
        $this->load->view("template",$data);

    }

    function get_fault_ajax()
    {
        $this->entrance();
        $this->load->model("Complain");
        $category = $_POST["category"];
        // print_r($this->Complain->get_faultlist("Dial Tone"));
         echo json_encode($this->Complain->get_faultlist($category));
    }

    function submit_complain()
    {
        $this->entrance();
        $_POST["since"] = date("Y-m-d",strtotime($_POST["since"]) );
        $this->load->model('Complain');
        if($this->Complain->already_complain($this->phoneno,$_POST["faultid"]))
        $this->confirm("You have already complainted for this fault.");
        $code = $this->Telephone->get_code($this->phoneno);
        $empid = $this->Employee->get_DE($code);
        $appid = $this->Complain->submit_complain($code,$empid,$this->phoneno,$_POST["faultid"],$_POST["other"],$_POST["since"]);
      // if($_POST["other"]==NULL)//

       $this->confirm("Your Application number is ".$appid.". Remember this and use for checking status of your Application.");



    }
    function download_report($appid)
    {
          
        
          $this->load->model('Newconnection');
          $this->load->helper('pdf');
          $data['data'] = $this->Newconnection->get_report($appid);
         // print_r($data['data']);
          $data['title']="Demand Note";
         $html = $this->load->view('report', $data,TRUE);
         //echo $html;
         pdf_create($html, 'demand_note','a4');
    }
    function logout()
    {
        $newdata = array(
                   'phoneno'  => "",
                   'rank'=> "",
                   'logged_in' => ""
               );

        $this->session->unset_userdata($newdata);
        $this->session->set_userdata('logged_in',false);
        redirect(home);
    }
    function complain_check_status()
    {
        $this->entrance();
        $data["title"]="Complain Check Status";
        $data["content"]="register/shift_check_status";
        $data["sidebar"]="admin/sidebar";
         $js[]="complain_check_status.js";
         $data["js"]=$js;
         $this->load->view("template",$data);
    }
    function complain_fetch_ajax()
    {
        $this->entrance();
        $this->load->model('Complain');
        echo json_encode($this->Complain->get_status($_POST));
    }
}
?>
